package com.lenovo.testreport.jenkins.controller;

import com.lenovo.testreport.jenkins.model.Job;
import com.lenovo.testreport.jenkins.service.jenkinsService;
import com.lenovo.testreport.user.model.MessageCenter;
import com.lenovo.testreport.user.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author CJF
 * @date 2018/12/7
 */
@Controller
@RequestMapping("/jenkins")
public class ExecutorJenkins {
    @Autowired
    jenkinsService jenkinsService;

    @Autowired
    private MessageService messageservice;

    /**
     * 执行构建
     *
     * @param request
     * @return
     */
    @RequestMapping("/job/build")
    @ResponseBody
    public boolean buildJob(HttpServletRequest request) {
        String jobName = request.getParameter("jobName");
        String user = request.getSession().getAttribute("User").toString();
        boolean result = jenkinsService.buildJob(jobName, false, "xxxx");
        messageservice.AddMessageRecord(new MessageCenter(user, "对Job：" + jobName + " 执行了构建", "normal-jenkins"));
        return result;
    }

    /**
     * 停止构建
     *
     * @param request
     * @return
     */
    @RequestMapping("/job/stop")
    @ResponseBody
    public boolean stopJob(HttpServletRequest request) {
        String jobName = request.getParameter("jobName");
        boolean result = jenkinsService.stopJob(jobName, false, "xxxx");
        String user = request.getSession().getAttribute("User").toString();
        messageservice.AddMessageRecord(new MessageCenter(user, "对Job：" + jobName + " 终止了构建", "normal-jenkins"));
        return result;
    }

    /**
     * 获取Job列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/job/list")
    @ResponseBody
    public List<Job> JobList(HttpServletRequest request) {
        String type = request.getParameter("type");
        return jenkinsService.getList(type);
    }

}
